<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/vendor/bootstrap/css/bootstrap.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath}/assets/vendor/font-awesome/css/font-awesome.min.css">
    <!-- Custom Font Icons CSS-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/font.css">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/custom.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/layer.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/bootstrap/css/bootstrap-switch.min.css">
    <title>查看用户</title>
</head>
<body>
<%@ include file="publicjsp/head.jsp" %>
<div class="d-flex align-items-stretch">
    <!-- Sidebar Navigation-->

        <%@ include file="publicjsp/sideHead.jsp"%>

    <!-- Sidebar Navigation end-->
    <div class="page-content">
        <div class="page-header">
            <div class="container-fluid">
                <h2 class="h5 no-margin-bottom">用户列表</h2>
            </div>
        </div>
        <section class="no-padding-bottom">
            <div class="title">
                <form class="form-inline" method="post" action="${pageContext.request.contextPath}/user/list">
                    <div class="form-group">
                        <label for="inlineFormInput" class="sr-only">Name</label>
                        <c:if test="${requestScope.search == null}">
                            <input id="inlineFormInput" type="text" name="search" placeholder="按名字查找"
                                   class="mr-sm-3 form-control">
                        </c:if>

                        <c:if test="${requestScope.search != null}">
                            <input id="inlineFormInput" value="${requestScope.search}" type="text" name="search"
                                   placeholder="按名字查找" class="mr-sm-3 form-control">
                        </c:if>
                    </div>
                    <div class="form-group">
                        <input type="submit" value="查询" class="btn btn-primary">
                    </div>
                </form>

            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>简介</th>
                        <th>操作</th>
                        <th>加关注</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${requestScope.userList}" var="user">
                        <tr>
                            <th scope="row">${user.userId}</th>
                            <td>${user.realName}</td>
                            <td>
                                <c:if test="${user.gender == 0}">
                                    男
                                </c:if>

                                <c:if test="${user.gender == 1}">
                                    女
                                </c:if>

                            </td>
                            <td>${user.age}</td>
                            <td>${user.introduce}</td>
                            <td>
                                <input id="dUser" data-id="${user.userId}" type="submit" value="详细信息"
                                       class="btn-xs btn-primary userDetail">
                            </td>

                            <td>
                                <c:if test="${user.ufId == null}">
                                    <input data-ufid="${user.ufId}" data-uid="${user.userId}" type="checkbox" value="" class="checkbox-template">
                                </c:if>

                                <c:if test="${user.ufId != null}">
                                    <input checked="checked" data-ufid="${user.ufId}" data-uid="${user.userId}" type="checkbox" value="" class="checkbox-template">
                                </c:if>
                            </td>
                        </tr>
                    </c:forEach>

                    </tbody>
                </table>

                <nav class="text-center" aria-label="Page navigation">
                    <ul id="pageUL" class="pagination">
                        <li>
                            <a id="pre" href="#" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <c:forEach var="index" varStatus="status" begin="${requestScope.startPage}"
                                   end="${requestScope.endPage}" step="1">
                            <c:if test="${status.count + (requestScope.startPage - 1) == requestScope.currentPage}">
                                <c:if test="${requestScope.search == null}">
                                    <li data-page="${index}"><a class="active" href="${pageContext.request.contextPath}/view/userList?page=${index}">${index}</a></li>
                                </c:if>

                                <c:if test="${requestScope.search != null}">
                                    <li data-page="${index}"><a class="active" href="${pageContext.request.contextPath}/view/userList?page=${index}&search=${requestScope.search}">${index}</a></li>
                                </c:if>
                            </c:if>

                            <c:if test="${status.count + (requestScope.startPage - 1) != requestScope.currentPage}">
                                <c:if test="${requestScope.search == null}">
                                    <li data-page="${index}"><a href="${pageContext.request.contextPath}/view/userList?page=${index}">${index}</a></li>
                                </c:if>

                                <c:if test="${requestScope.search != null}">
                                    <li data-page="${index}"><a href="${pageContext.request.contextPath}/view/userList?page=${index}&search=${requestScope.search}">${index}</a></li>
                                </c:if>
                            </c:if>

                        </c:forEach>

                        <li>
                            <a id="next" href="#" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </section>

        <%@ include file="publicjsp/footer.jsp" %>
    </div>
</div>

<!-- JavaScript files-->
<script src="${pageContext.request.contextPath}/assets/vendor/jquery/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/vendor/popper.js/umd/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/bootstrap/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/vendor/jquery.cookie/jquery.cookie.js"></script>
<script src="${pageContext.request.contextPath}/assets/vendor/chart.js/Chart.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/js/layer.js"></script>
<script src="${pageContext.request.contextPath}/assets/vendor/jquery-validation/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/js/charts-home.js"></script>
<script src="${pageContext.request.contextPath}/assets/js/front.js"></script>
<script src="${pageContext.request.contextPath}/assets/js/custom.js"></script>


<script>

    $(function () {
        $("#pre").click(function () {
            var li = $("#pageUL>li:nth-child(2)");
            console.log("找到第二个孩子li：" + li.html());
            console.log(li.attr("data-page"));
            var firstPage = li.attr("data-page");
            if ('1' === firstPage) {
                layer.msg("前面没有页码了");
                return;
            }
            console.log(Number(firstPage));
            var search = '${requestScope.search}';
            if (null == search) {
                window.location.href = '${pageContext.request.contextPath}/view/userList?page=' + (Number(firstPage) - 1);
            } else {
                window.location.href = '${pageContext.request.contextPath}/view/userList?page=' + (Number(firstPage) - 1) + '&search=' + search;
            }
        });

        $("#next").click(function () {
            var li = $("#pageUL>li:nth-child(2)");
            console.log("找到第二个孩子li：" + li.html());
            var firstPage = li.attr("data-page");
            var maxPage = Number(firstPage) + 4;
            var totalPage = '' + ${requestScope.totalPage};
            console.log(maxPage + "---" + totalPage);
            if (Number(totalPage) <= maxPage) {
                layer.msg("后面没有页码了");
                return;
            }
            console.log(Number(firstPage));
            var search = '${requestScope.search}';
            if (null == search) {
                window.location.href = '${pageContext.request.contextPath}/view/userList?page=' + (Number(firstPage) + 5);
            } else {
                window.location.href = '${pageContext.request.contextPath}/view/userList?page=' + (Number(firstPage) + 5) + '&search=' + search;
            }
        })

        //用户列表，查看详情，通过class名字userDetail来找
        $(".userDetail").click(function () {
            console.log("你点击了用户详情");
            var userId = $(this).attr("data-id");
            window.location.href = '${pageContext.request.contextPath}/user/getDetail?userId=' + userId;
        });

    })

</script>

</body>
</html>